package com.cn.hangyun.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.hangyun.common.JsonUtil;
import com.cn.hangyun.model.Member;
import com.cn.hangyun.service.IMemberService;


@Controller
@RequestMapping("/member")
public class MemberController {

	private IMemberService memberService;

	@Autowired
	public void setUserService(IMemberService memberService) {
		this.memberService = memberService;
	}

	@RequestMapping("/login")
	@ResponseBody
	public void showUser(String userNm,String userPw,HttpServletResponse response) throws IOException {
		Member member = memberService.getMemberByNm(userNm);
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		if (null != member && !(member.getUserNm().isEmpty())) {
			if (userPw.equals(member.getUserPw())) {
				//登录成功
				rmap.put("loninStatus", 0);
				rmap.put("perm", member.getUserPermission());
			} else {
				//密码不正确
				rmap.put("loninStatus", 1);
			}
		} else {
			//用户不存在
			rmap.put("loninStatus", 2);
		}
		String jsonString = JsonUtil.toJson(rmap);
		ServletOutputStream os = response.getOutputStream();
		os.write(jsonString.getBytes(Charset.forName("UTF-8")));
		os.flush();
	}
	
	@RequestMapping("/updatepss")
	@ResponseBody
	public void updatePss(Member memberParam,HttpServletResponse response) throws IOException {

		int recCount = memberService.updateUserPw(memberParam);
		Map<Object, Object> rmap = new HashMap<Object, Object>();
		if (recCount > 0) {
			//更新成功
			rmap.put("updateStatus", 0);
		} else {
			//更新失败
			rmap.put("updateStatus", 1);
		}
		String jsonString = JsonUtil.toJson(rmap);
		ServletOutputStream os = response.getOutputStream();
		os.write(jsonString.getBytes(Charset.forName("UTF-8")));
		os.flush();
	}
	@RequestMapping("/selectAllUser")
	@ResponseBody
	public void selectUser(Member memberParam,HttpServletResponse response) throws IOException {

		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject reJson = new JSONObject();
		List<Member> list = memberService.getMemberAll();
		
		Map mTotal = new HashMap();
		mTotal.put("rows", list);
		reJson.putAll(mTotal);
		out.println(reJson);
	}
	@RequestMapping("/saveUser")
	@ResponseBody
	public void saveUser(HttpServletRequest request,HttpServletResponse response) throws IOException {

		String ens = request.getParameter("entities");
		if (null != ens) {
			 ens = "[" + ens.toString() + "]";
			 JSONArray array =JSONArray.parseArray(ens.toString());
			 for (int i=0;i<array.size();i++) {
	
				 Member memberP=JSON.parseObject((String)array.get(i).toString(), Member.class);
				 //存在性check
				 Member isExist=memberService.getMemberByid(memberP.getUid());
				 if (null == isExist) {
					 memberP.setUserPw("8888");
					 Timestamp tp= new Timestamp(System.currentTimeMillis());
					 memberP.setInsertTime(tp);
					 memberP.setUpdateTime(tp);
					 memberService.insert(memberP);
				 } else {
					 memberP.setUpdateTime(new Timestamp(System.currentTimeMillis()));
					 memberService.updateMember(memberP);
				 }
				// System.out.println("aaaaaaaaaaa");
			 }
		}
	}
	@RequestMapping("/delete")
	@ResponseBody
	public void delete(String uid,HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		memberService.deleteById(Integer.valueOf(uid));
//		Map mTotal = new HashMap();
//		mTotal.put("result", "yes");
//		reJson.putAll(mTotal);
//		out.println(reJson);
	}
}
